package cn.school.demo.ui.my

import android.graphics.Color
import android.os.Bundle
import android.view.*
import androidx.fragment.app.Fragment
import androidx.lifecycle.ViewModelProvider
import androidx.navigation.fragment.findNavController
import androidx.navigation.ui.setupWithNavController
import cn.school.demo.MyApplication
import cn.school.demo.R
import cn.school.demo.databinding.FragmentMyBinding
import com.blankj.utilcode.util.LogUtils
import com.google.android.material.bottomnavigation.BottomNavigationView
import com.mylhyl.circledialog.CircleDialog

class MyFragment : Fragment() {
    private lateinit var viewModel: MyViewModel
    private var _binding: FragmentMyBinding? = null
    private val binding get() = _binding!!

    override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View {
        viewModel = ViewModelProvider(this).get(MyViewModel::class.java)
        _binding = FragmentMyBinding.inflate(inflater, container, false)
        val root: View = binding.root
        LogUtils.d(MyApplication.user)

        val navView: BottomNavigationView = binding.navView
        val navController = findNavController()
        navView.setupWithNavController(navController)


        binding.user = MyApplication
        binding.view = this
        binding.lifecycleOwner = viewLifecycleOwner
        MyApplication.apply {
            systemBarSetting(MyApplication.LIGHT, requireActivity())
            setCircleGlideByUrlString(
                requireContext(),
                user!!.headPortrait,
                binding.mainMyFragmentAvatar
            )
        }

        return root
    }


    override fun onDestroyView() {
        super.onDestroyView()
        _binding = null
    }

    fun mainMyFragmentEditOnClick() {
        MyApplication.apply {
            navigateThing["My"] = "MyInfo"
            goto(R.id.action_My_to_MyInfo, findNavController(), requireFragmentManager())
        }
    }


    fun mainMyFragmentCollectionOnClick() =
        MyApplication.goto(R.id.action_My_to_Collect, findNavController(), requireFragmentManager())

    fun mainMyFragmentHistoryOnClick() {
        MyApplication.apply {
            LogUtils.e("我要去历史界面了！")
            navigateThing["History"] = R.id.action_History_to_My.toString()
            goto(R.id.action_My_to_History, findNavController(), requireFragmentManager())
        }
    }


    fun mainMyFragmentSignOnClick() {
        //签到按钮
        viewModel.apply {
            userSign()
            result.observe(viewLifecycleOwner) {
                LogUtils.d("我坚挺到了变化啊啊啊啊!!")
                when (it.success) {
                    true -> {
                        MyApplication.user!!.totalIntegral += 1
                        CircleDialog.Builder()
                            .setText("签到成功！")
                            .setNegative("确定", null)
                            .show(fragmentManager)
                    }
                    else -> CircleDialog.Builder()
                        .setText(it.message)
                        .setNegative("确定", null)
                        .show(fragmentManager)
                }
            }
        }
    }

    fun mainMyFragmentPointsLeaderboardOnClick() =
        MyApplication.goto(
            R.id.action_My_to_PointRanking,
            findNavController(),
            requireFragmentManager()
        )

    fun mainMyFragmentPointsBreakdownOnClick() =
        MyApplication.goto(
            R.id.action_My_to_PointDetail,
            findNavController(),
            requireFragmentManager()
        )

    fun mainMyFragmentPartyHistoryAnswersOnClick() =
        MyApplication.goto(
            R.id.action_My_to_PartyHistoryAnswer,
            findNavController(),
            requireFragmentManager()
        )


    fun mainMyFragmentSetHobbiesOnClick() {
        MyApplication.navigateThing["SetLove"] = "MyInfo to SetLove"
        MyApplication.goto(
            R.id.action_My_to_SetLove,
            findNavController(),
            requireFragmentManager()
        )
    }

    fun mainMyFragmentSettingOnClick() =
        MyApplication.goto(R.id.action_My_to_Setting, findNavController(), requireFragmentManager())

}